package com.caszx.chargingpile.fragment.reservate;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.app.base.adapter.CommonViewPageAdapter;
import com.app.base.fragment.BaseFragment;
import com.app.utils.DipPxUtil;
import com.app.utils.SystemHelper;
import com.app.view.PagerSlidingTabStrip;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.eventbus.EventBusAction;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 电站预约
 */
public class ReservateHomeFragment extends BaseFragment {

    //private Toolbar mToolbar;
   // private EditText mEditCenterView;
    private TextView txt_center_title;
    private List<Fragment> pagers = new ArrayList<Fragment>(3);
    private CommonViewPageAdapter mAdapter;
    private ViewPager viewpager;
    private PagerSlidingTabStrip viewpageTabs;

    public static ReservateHomeFragment newInstance() {
        ReservateHomeFragment fragment = new ReservateHomeFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected View initView() {
        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_reservate_home, null);
        return  view;
    }


    public void onEvent(String action){
        if(EventBusAction.ACTION_USER_SUCCESS_RESER.equals(action)){
            //预约成功
            viewpager.setCurrentItem(1);
        }
    }
    private void initToolBar(View view){
        txt_center_title = (TextView) view.findViewById(R.id.center_title);
        txt_center_title.setText("预约");
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(null);
//        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
//        mToolbar.setNavigationIcon(null);
//        mToolbar.inflateMenu(R.menu.menu_search);
    }


    private void initCenterView(View view){
//        mEditCenterView = (EditText) view.findViewById(R.id.center_title);
//        ViewGroup.LayoutParams layoutParams =  mEditCenterView.getLayoutParams();
//        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
//        layoutParams.height = (int) (SystemHelper.getActionBarHeight(getActivity())-DipPxUtil.dip2px(getActivity(),10));
//        mEditCenterView.setCompoundDrawablePadding(DipPxUtil.dip2px(getActivity(),6));
//        mEditCenterView.setPadding(DipPxUtil.dip2px(getActivity(),6),0,0,0);
//        mEditCenterView.setBackgroundResource(R.drawable.shap_rectangle_stroke_bg);
//        Drawable drawable = getResources().getDrawable(R.mipmap.list_location_icon);
//        drawable.setBounds(0,0, DipPxUtil.dip2px(getActivity(),30),DipPxUtil.dip2px(getActivity(),30));
//        mEditCenterView.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);
//        mEditCenterView.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initToolBar(view);
        initCenterView(view);
        OftenUsedFragment oftenUsed = OftenUsedFragment.newInstance();
        ReservateIngFragment ing    = ReservateIngFragment.newInstance();
        RecordFragment record       = RecordFragment.newInstance();
        pagers.add(oftenUsed);
        pagers.add(ing);
        pagers.add(record);
        initViewPage(view);
    }

    private void initViewPage(View view) {
        viewpager = (ViewPager) view.findViewById(R.id.viewpager);
        viewpager.setOffscreenPageLimit(pagers.size());
        mAdapter = new CommonViewPageAdapter(getChildFragmentManager());
        mAdapter.setData(pagers);
        String[] tabs = getResources().getStringArray(R.array.reservate_tab);
        mAdapter.setPageTitles(tabs);
        viewpager.setAdapter(mAdapter);
        viewpageTabs = (PagerSlidingTabStrip) view.findViewById(R.id.indicator_tab);
        setTabsValue(viewpager);
    }

    protected void setTabsValue(ViewPager viewpage) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        viewpageTabs.setViewPager(viewpage);
        // 设置Tab是自动填充满屏幕的
        viewpageTabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        viewpageTabs.setDividerColor(Color.TRANSPARENT);
        // 设置Tab底部线的高度
        viewpageTabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        viewpageTabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 2, dm));
        // 设置Tab标题文字的大小
        viewpageTabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, dm));
        // 设置Tab Indicator的颜色
        viewpageTabs.setIndicatorColor(getResources().getColor(R.color.colorAccent));
        // 设置选中Tab文字的颜色
        viewpageTabs.setSelectedTextColor(getResources().getColor(R.color.colorAccent));
        // 取消点击Tab时的背景色
        viewpageTabs.setTabBackground(0);
        viewpageTabs.setBackgroundColor(getResources().getColor(R.color.white));
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
